package com.dg.river.core.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

import com.dg.river.R;

/**
 * Created by hy on 2023/04/11
 * Description : 补费弹窗
 */
public class SupplementaryFeeDialog extends Dialog {

    public SupplementaryFeeDialog(Context context) {
        super(context);
    }

    public SupplementaryFeeDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context mContext;
        private boolean cancel;
        private SupplementaryFeeListener listener;
        private SupplementaryFeeCancelListener supplementaryFeeCancelListener;
        private SupplementaryFeeDialog dialog;

        public Builder(Context mContext) {
            this.mContext = mContext;
        }

        /**
         * 设置是否能够取消
         *
         * @param bool
         * @return
         */
        public Builder setCancel(boolean bool) {
            this.cancel = bool;
            return this;
        }

        public interface SupplementaryFeeListener {
            void onPay(SupplementaryFeeDialog dialog);
        }

        public Builder setSupplementaryFeeListener(SupplementaryFeeListener listener) {
            this.listener = listener;
            return this;
        }

        public interface SupplementaryFeeCancelListener {
            void onCancel(SupplementaryFeeDialog dialog);
        }

        public Builder setCancelListener(SupplementaryFeeCancelListener supplementaryFeeCancelListener) {
            this.supplementaryFeeCancelListener = supplementaryFeeCancelListener;
            return this;
        }

        /**
         * 创建
         *
         * @return
         */
        public SupplementaryFeeDialog create() {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            dialog = new SupplementaryFeeDialog(mContext, R.style.dialog_normal);
            View layout = inflater.inflate(R.layout.dialog_supplementary_fee, null);
            dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            dialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);

            TextView tvSupplementary = layout.findViewById(R.id.tvSupplementary);
            ImageView ivClose = layout.findViewById(R.id.ivClose);

            ivClose.setOnClickListener(v -> {
                if (supplementaryFeeCancelListener != null) {
                    supplementaryFeeCancelListener.onCancel(dialog);
                }
                dialog.dismiss();
            });
            tvSupplementary.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onPay(dialog);
                }
            });
            dialog.setContentView(layout);
            if (!cancel) {
                dialog.setCancelable(false);
            } else {
                dialog.setCancelable(true);
            }
            return dialog;
        }
    }
}
